package ui {
import core.AGameObject;
import core.Facade;

import flash.display.Loader;

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

import ru.cjay.loading.ResourceManager;
import ru.cjay.loading.UnsafeConnection;

import utils.TraceObject;


public class DigLevelUI extends AGameObject {

    private var _setting:int;

    private var _zone_id:int;
    private var _game_id:int;
    private var _cash:int;
    private var _cash_full:String;
    private var _cash_short:String;
    private var _col:int;
    private var _col_full:String;
    private var _col_short:String;
    private var _jackpot:int;
    private var _jackpot_full:String;
    private var _jackpot_short:String;
    private var _jackpot_hint:String;
    private var _cash_cost:int;
    private var _cash_cost_full:String;
    private var _cash_cost_short:String;
    private var _col_cost:int;
    private var _col_cost_full:String;
    private var _col_cost_short:String;
    private var _prize:Object;
    private var _size_x:int;
    private var _size_y:int;
    private var _winner:Object;
    private var _rating_clicks:Object;
    private var _rating_wins:Object;
    private var _click_result:int;
    private var _next_update:int;
    private var _cells:Array;

    private var _ui:MovieClip;

    public function get ui():MovieClip {
        return _ui;
    }

    public function set ui(value:MovieClip):void {
        _ui = value;
    }

    public function DigLevelUI(level:DigLevel) {
        _setting = level.setting;

        _ui = new (ResourceManager.getInstance().getSwfAssetClass("main_assets_"+_setting+":UIPanel")as Class)();
        _ui.x =  180;
        _ui.y =  13;

        _ui.in_game_caption.text = level.strings_user_count_hint;
        _ui.player_count.text = level.strings_user_count+" "+level.strings_user_count_suf;
//        trace("Level caption = ", level.caption);
        _ui.caption.text = level.caption;
        _ui.bonus_name.text = level.prize.name;

        _ui.rubl_switch.buttonMode = true;
        _ui.rubl_switch.useHandCursor = true;

        _ui.tal_switch.buttonMode = true;
        _ui.tal_switch.useHandCursor = true;

        _ui.rubl_switch.addEventListener(MouseEvent.CLICK, switchToRouble);
        _ui.tal_switch.addEventListener(MouseEvent.CLICK, switchToTal);

        if (Facade.getInstance().selected_monetype=="col") {
            _ui.rubl_switch.gotoAndStop(1);
            _ui.tal_switch.gotoAndStop(2);
        }else{
            _ui.rubl_switch.gotoAndStop(2);
            _ui.tal_switch.gotoAndStop(1);
        }



        _ui.rubl_switch.addEventListener(MouseEvent.ROLL_OVER, btn_hover_on);
        _ui.rubl_switch.addEventListener(MouseEvent.ROLL_OUT, btn_hover_off);

        _ui.tal_switch.addEventListener(MouseEvent.ROLL_OVER, btn_hover_on);
        _ui.tal_switch.addEventListener(MouseEvent.ROLL_OUT, btn_hover_off);

        if (level.prize.prize_type=="specialitem"){
            _ui.atack_label.visible = false;
            _ui.defense_label.visible = false;
            _ui.defense_value.visible = false;
            _ui.atack_value.visible = false;
        }else{
            _ui.atack_value.text = level.prize.attack;
            _ui.defense_value.text = level.prize.defense;
        }



        var icon:Sprite = UnsafeConnection.UnsafeLoader(level.prize.img,32,32);
        _ui.unit_icon.img.addChild(icon);

        _ui.rubl_cost_label.text = level.strings_in_cash;
        _ui.talon_cost_label.text = level.strings_in_col;
        _ui.cost_rubls.text =  level.cash_cost_short;
        _ui.cost_talons.text =  level.col_cost_short;

        _ui.price_result.text = "+"+level.jackpot_full;

        addChild(_ui);

    }

    public function setJackpot(str:String):void{
        _ui.price_result.text = "+"+str;
    }

    public function setPlayersCount(mess:String):void{
//        _ui.player_count.text = level.strings_user_count+" "+level.strings_user_count_suf;
        _ui.player_count.text = mess;
    }

    private function btn_hover_on(event:MouseEvent):void{
        (event.target as MovieClip).gotoAndStop(2);
    }
    private function btn_hover_off(event:MouseEvent):void{
        if ((Facade.getInstance().selected_monetype=="col")==(event.target == _ui.rubl_switch) )
            (event.target as MovieClip).gotoAndStop(1);

    }

    private function  switchToRouble(event:MouseEvent):void{
        if (Facade.getInstance().selected_monetype=="col"){
            _ui.rubl_switch.gotoAndStop(2);
            _ui.tal_switch.gotoAndStop(1);
            Facade.getInstance().selected_monetype="cash"
        }

    }
    private function  switchToTal(event:MouseEvent):void{
         if (Facade.getInstance().selected_monetype=="cash"){
            _ui.tal_switch.gotoAndStop(2);
             _ui.rubl_switch.gotoAndStop(1);
            Facade.getInstance().selected_monetype="col"
        }
    }



}
}
